var chargeStartedApp = angular.module("chargeStarted", []);
chargeStartedApp.controller("chargeStartedController", function($scope, $rootScope, $window, scCodeService, Popup, $location) {
    //是否正在充电
    var openCharge = localStorage.getItem("openCharge");
    //获取token
    var token = localStorage.getItem("token");
    //获取用户卡号
    var userCard = localStorage.getItem("cardNo");
    //獲取用戶id
    var userId = localStorage.getItem("userId");
    // alert(openCharge);
    //如果正在充电就跳转
    if (openCharge == "true") {
        $location.path("/charging");
    } else {
        //监听againScanController获取桩详情
        // $scope.$watch(function() {
        //     return $window.chargerDetail;
        // });
        // $scope.chargerDetail = $window.chargerDetail;
        $scope.chargerDetail = JSON.parse(localStorage.getItem("chargerDetail"));
        //alert("开始充电"+JSON.stringify($scope.chargerDetail));
        // localStorage.setItem("chargerDetail", JSON.stringify($scope.chargerDetail));
    }
    //判断是否通过二维码进入开始充电页面
    if ($scope.chargerDetail == undefined||$scope.chargerDetail == null) {
        $location.path("/manualCode");
    } else {
         //alert("桩号"+JSON.stringify($scope.chargerDetail));
        //遍历plugNoList
        var plugNoList = $scope.chargerDetail.plugNoList;
        for (var i = 0; i < plugNoList.length; i++) {
            var plugNoList = plugNoList[i];
        }
        $scope.chargerDetail.chargeStatus = plugNoList.chargeStatus;
        //chargerId and plugNo
        localStorage.setItem("chargerId", $scope.chargerDetail.chargerId);
        localStorage.setItem("plugNo", plugNoList.portNo);
        //获取费率
        $scope.getRate = function() {
            var postData = {
                chargerId: $scope.chargerDetail.chargerId
            }
            // alert("桩号"+JSON.stringify($scope.chargerDetail));
            scCodeService.getRate(postData).then(function(data) {
                //费率处理
                for (var i = 0; i < data.data.length; i++) {
                    if (data.data[i].feeType == 3) {
                        $scope.serveRate = data.data[i].rate;
                    }
                }
                for (var i = 0; i < data.data.length; i++) {
                    if (data.data[i].feeType == 2) {
                        $scope.chargerRate = data.data[i].rate;
                    }
                }
                localStorage.setItem("rate", $scope.chargerRate);
            })

        }
        $scope.getRate();
        //开启充电
        //判断桩的详情
        if (plugNoList.chargeStatus == 0) {
            
            $scope.openCharge = function() {
                document.getElementById("loadingToast").style.display = "block";
                var postData = {
                    chargerId: $scope.chargerDetail.chargerId,
                    userCard: userCard,
                    chargerPlugNo: plugNoList.portNo,
                    token: token,
                    source:mainConfig.source,
                    merchantCode:mainConfig.merchantCode
                };
                scCodeService.openCharge(postData).then(function(data) {
                    if (data.code = "200") {
                        document.getElementById("loadingToast").style.display = "none";
                        //获取开始时间
                        var temp = new Date();
                        var Y = temp.getFullYear() + '-';
                        var M = (temp.getMonth()+1 < 10 ? '0'+(temp.getMonth()+1) : temp.getMonth()+1) + '-';
                        var D = temp.getDate() + ' ';
                        var h = temp.getHours();
                        if(h<10){
                            h = "0" + h;
                        }
                        var m = temp.getMinutes();
                        if(m<10){
                            m = "0" + m;
                        }
                        var s = temp.getSeconds();
                        if(s<10){
                            s = "0" + s;
                        } 
                        var result = Y+M+D+h+":"+m+":"+s;
                        var SMS = Date.parse(temp);
                        // var regex = /\//g;
                        // var result = (temp.toLocaleDateString("zh-cn") + ' ' + temp.toLocaleTimeString("en-GB",{hour12: false})).replace(regex, '-');
                        localStorage.setItem("SMS", SMS);
                        console.log(localStorage.getItem("SMS"));
                        localStorage.setItem("startTime", result);
                        //判断充电开启状态
                        $scope.openCharge = "true";
                        localStorage.setItem("openCharge", $scope.openCharge);
                        $location.path("/charging");
                    } else {
                        console.log("+++！！！");
                        document.getElementById("loadingToast").style.display = "none";
                        Popup.notice('请求失败', 3000, function() {
                            console.log('ok')
                        });
                    }
                })
            }
        } else if (plugNoList.chargeStatus == 2) {
            Popup.notice('充电桩使用中', 3000, function() {
                console.log('ok')
            });
        } else {
            Popup.notice('充电桩不可用', 3000, function() {
                console.log('ok')
            });
        }
    }
})
